package team.seekers.hula.question.answerComment.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.util.StringUtils;
import team.seekers.hula.common.entity.PageResult;
import team.seekers.hula.common.entity.enums.NoticeTypeEnum;
import team.seekers.hula.question.answerComment.entity.dto.AnswerCommentAddDTO;
import team.seekers.hula.question.answerComment.entity.dto.AnswerCommentQueryDTO;
import team.seekers.hula.auth.service.MindUtils;
import team.seekers.hula.question.answer.entity.dao.AnswerDO;
import team.seekers.hula.question.answerComment.entity.dao.AnswerCommentDO;
import team.seekers.hula.question.answer.service.AnswerService;
import team.seekers.hula.message.message.service.MessageService;
import team.seekers.hula.user.user.entity.dao.UserDO;
import team.seekers.hula.message.message.entity.dto.MessageAddDTO;
import team.seekers.hula.question.answerComment.entity.vo.AnswerCommentListVO;
import team.seekers.hula.question.answerComment.entity.dto.AnswerCommentUpdateDTO;
import team.seekers.hula.question.answerComment.mapper.AnswerCommentMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import team.seekers.hula.user.user.service.UserService;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 回答的评论 服务实现类
 * </p>
 *
 * @author hbc
 * @since 2021-02-18
 */
@Service
public class AnswerCommentService extends ServiceImpl<AnswerCommentMapper, AnswerCommentDO> {

    private boolean result = true;

    @Autowired
    @Lazy
    private AnswerService answerService;
    @Autowired
    @Lazy
    private UserService userService;
    @Autowired
    @Lazy
    private AnswerCommentGoodService answerCommentGoodService;
    @Autowired
    @Lazy
    private MessageService messageService;

    public PageResult<AnswerCommentListVO> page(long current, long limit, AnswerCommentQueryDTO answerCommentQueryDTO) {
        Page<AnswerCommentDO> page = new Page<>(current,limit);
        QueryWrapper<AnswerCommentDO> wrapper = new QueryWrapper<>();
        String answerId = answerCommentQueryDTO.getAnswerId();
        wrapper.like("answer_id",answerId);

        String begin = answerCommentQueryDTO.getBegin();
        if(!StringUtils.isEmpty(begin)){
            wrapper.ge("gmt_create",begin);
        }
        String end = answerCommentQueryDTO.getEnd();
        if (!StringUtils.isEmpty(end)){
            wrapper.le("gmt_create",end);
        }

        if(answerCommentQueryDTO.getSortBy() == null || answerCommentQueryDTO.getSortBy() != 1){
            wrapper.orderByDesc("good_number");
        }else {
            wrapper.orderByDesc("gmt_create");
        }

        page(page,wrapper);
        long total = page.getTotal();
        List<AnswerCommentDO> records = page.getRecords();
        if(records == null){
            new PageResult<AnswerCommentListVO>();
        }
        List<AnswerCommentListVO> list = new ArrayList<>();
        for (AnswerCommentDO record : records) {
            AnswerCommentListVO answerCommentListVO = new AnswerCommentListVO();
            BeanUtils.copyProperties(record,answerCommentListVO);
            UserDO userDO = userService.getById(record.getUserId());
            if(!record.getUserId().equals(MindUtils.getUserId())){
                answerCommentListVO.setIsMine(true);
            }
            answerCommentListVO.setNickname(userDO.getNickname());
            answerCommentListVO.setAvatar(userDO.getAvatar());

            answerCommentListVO.setIsLiked(answerCommentGoodService.hasGood(answerCommentListVO.getId()));

            list.add(answerCommentListVO);
        }
        return new PageResult<>(total,list);
    }

    public String add(AnswerCommentAddDTO answerCommentAddDTO) {
        // 维护回答评论数
        answerService.addComment(answerCommentAddDTO.getAnswerId());

        // 新增评论
        AnswerCommentDO answerCommentDO = new AnswerCommentDO();
        BeanUtils.copyProperties(answerCommentAddDTO, answerCommentDO);
        answerCommentDO.setUserId(MindUtils.getUserId());
        AnswerDO answerDO = answerService.getById(answerCommentAddDTO.getAnswerId());
        answerCommentDO.setQuestionId(answerDO.getQuestionId());
        save(answerCommentDO);

        // 给回答者发送审核信息
        MessageAddDTO messageAddDTO = new MessageAddDTO();
        messageAddDTO.setQuestionId(answerDO.getQuestionId());
        messageAddDTO.setLinkId(answerCommentDO.getId());
        messageAddDTO.setToId(answerDO.getUserId());
        messageAddDTO.setType(NoticeTypeEnum.ADD_ANSWER_COMMENT);
        messageService.add(messageAddDTO);

        return answerCommentDO.getId();
    }

    public void delete(String id) {
        // 判断是不是该用户的评论
        AnswerCommentDO answerCommentDO = getById(id);
        if(!MindUtils.isAdmin()){
            if(!answerCommentDO.getUserId().equals(MindUtils.getUserId())){
                return;
            }
        }
        // 维护回答评论数
        answerService.reduceComment(answerCommentDO.getAnswerId());

        //删除评论的所有赞
        answerCommentGoodService.deleteByAnswerCommentId(answerCommentDO.getId());

        // 删除评论
        removeById(id);
    }

    public void update(AnswerCommentUpdateDTO updateVO) {
        // 判断是不是该用户的评论
        AnswerCommentDO answerCommentDO = getById(updateVO.getId());
        if(!MindUtils.isAdmin()){
            if(!answerCommentDO.getUserId().equals(MindUtils.getUserId())){
                return;
            }
        }
        //更新评论
        BeanUtils.copyProperties(updateVO, answerCommentDO);
        updateById(answerCommentDO);
    }

    public void addGood(String answerCommentId) {
        AnswerCommentDO answerCommentDO = getById(answerCommentId);
        if(answerCommentDO == null){
            return ;
        }
        answerCommentDO.setGoodNumber(answerCommentDO.getGoodNumber()+1);
        updateById(answerCommentDO);
    }

    public void reduceGood(String answerCommentId) {
        AnswerCommentDO answerCommentDO = getById(answerCommentId);
        if(answerCommentDO == null){
            return;
        }
        answerCommentDO.setGoodNumber(answerCommentDO.getGoodNumber()-1);
        updateById(answerCommentDO);
    }

    public void deleteByAnswerId(String answerId) {
        QueryWrapper<AnswerCommentDO> wrapper = new QueryWrapper<>();
        wrapper.eq("answer_id",answerId);
        List<AnswerCommentDO> list = list(wrapper);
        for (AnswerCommentDO answerCommentDO : list) {
            // 维护回答评论数
            answerService.reduceComment(answerCommentDO.getAnswerId());

            // 删除评论的所有赞
            answerCommentGoodService.deleteByAnswerCommentId(answerCommentDO.getId());

            // 删除评论
            removeById(answerCommentDO.getId());

        }
    }
}
